
package qldc.pm.qlktcsdldc.actions;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.portlet.PortletConfig;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;

import qldc.pm.qlktcsdldc.data.UtilsData;
import qldc.pm.qlktcsdldc.dto.LichSuDTO;
import qldc.pm.qlktcsdldc.utils.FLog;
import qldc.pm.qtht.dmdc.model.PhuongXa;
import qldc.pm.qtht.dmdc.model.QuanHuyen;
import qldc.pm.qtht.dmdc.service.PhuongXaLocalServiceUtil;
import qldc.pm.qtht.dmdc.service.QuanHuyenLocalServiceUtil;

import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.json.JSONArray;
import com.liferay.portal.kernel.json.JSONFactoryUtil;
import com.liferay.portal.kernel.json.JSONObject;
import com.liferay.portal.kernel.util.JavaConstants;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.Validator;

public class ResourceRequestHandler {

	public void loadQuanHuyen(ResourceRequest request, ResourceResponse response)
		throws IOException {

		// FLog.Log(TAG + " loadQuanHuyen");
		PrintWriter pw = response.getWriter();

		JSONArray jsonArray = JSONFactoryUtil.createJSONArray();

		long tinhIdSelected = ParamUtil.getLong(request, "tinhIdSelected");
		// FLog.Log(TAG + " loadQuanHuyen tinhIdSelected:" + tinhIdSelected);
		if (tinhIdSelected > 0) {
			List<QuanHuyen> quanHuyens = QuanHuyenLocalServiceUtil.findByTinhId(tinhIdSelected);
			JSONObject jsonObject;
			if (Validator.isNotNull(quanHuyens)) {
				for (QuanHuyen quanHuyen : quanHuyens) {
					jsonObject = JSONFactoryUtil.createJSONObject();
					jsonObject.put("quanHuyenId", quanHuyen.getQuanHuyenId());
					jsonObject.put("tenQuanHuyen", quanHuyen.getTenQuanHuyen());

					jsonArray.put(jsonObject);
				}
			}
		}

		pw.print(jsonArray.toString());
		pw.flush();
	}

	public void loadPhuongXa(ResourceRequest request, ResourceResponse response)
		throws IOException {

		// FLog.Log(TAG + " loadPhuongXa ");
		PrintWriter pw = response.getWriter();

		JSONArray jsonArray = JSONFactoryUtil.createJSONArray();

		long quanHuyenIdSelected = ParamUtil.getLong(request, "quanHuyenIdSelected");
		// FLog.Log(TAG + " loadPhuongXa quanHuyenIdSelected:" +
		// quanHuyenIdSelected);
		if (quanHuyenIdSelected > 0) {
			List<PhuongXa> phuongXas = PhuongXaLocalServiceUtil.findByQuanHuyenId(quanHuyenIdSelected);

			JSONObject jsonObject;
			if (Validator.isNotNull(phuongXas)) {
				for (PhuongXa phuongXa : phuongXas) {
					jsonObject = JSONFactoryUtil.createJSONObject();
					jsonObject.put("phuongXaId", phuongXa.getPhuongXaId());
					jsonObject.put("tenPhuongXa", phuongXa.getTenPhuongXa());

					jsonArray.put(jsonObject);
				}
			}
		}

		pw.print(jsonArray.toString());
		pw.flush();
	}
	
	
	public void onChangeThongTinThayDoi(ResourceRequest request, ResourceResponse response)
			throws IOException, SystemException {

		FLog.Log(" onChangeThongTinThayDoi ");
		long thongTinThayDoiId = Long.parseLong(ParamUtil.getString(request, "thongTinThayDoiId"));
		String soDinhDanhCaNhan = ParamUtil.getString(request, "soDinhDanhCaNhan");
		
		FLog.Log(" onChangeThongTinThayDoi  :"+ thongTinThayDoiId + " >"+ soDinhDanhCaNhan);
		 
		String giaTriCu = "";
		
		PortletConfig portletConfig = (PortletConfig) request.getAttribute(JavaConstants.JAVAX_PORTLET_CONFIG);
		List<LichSuDTO> lichSuDTOs = UtilsData.getLichSu(soDinhDanhCaNhan, thongTinThayDoiId, request.getLocale(), portletConfig);
		
		PrintWriter pw = response.getWriter();	
		JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
		if(Validator.isNotNull(lichSuDTOs) && lichSuDTOs.size() > 0){
			giaTriCu = lichSuDTOs.get(0).getGiaTriMoi();
		}
		
		jsonObject.put("giaTriCu", giaTriCu);
		pw.print(jsonObject.toString());
		pw.flush();
	}
}
